#!/bin/bash

PW=$(cd `dirname $0`;pwd)
cd $PW

export `grep 'export' ../env/base.groovy|sed 's#//##g'|awk '{print $2}'`
export `egrep 'ssl_key|url_url' ../env/base.groovy|sed 's#env.##g;s#"##g'`

cert_ssl=`ls ./ssl/|egrep "pem|crt"`
key_ssl=`ls ./ssl/|grep key`

#创建名称空间
function create_ns {
    nsname=`kubectl get ns|grep -w $ns|awk '{print $1}'|awk '{gsub(/^\s+|\s+$/, "");print}'`
    if [ "$nsname" != "$ns" ];then
        kubectl create ns $ns
        echo -e "\033[32;32m名称空间创建成功 \033[0m \n"
    else
        echo -e "\033[32;32m名称空间已存在 \033[0m \n"
    fi
}
#创建docker镜像仓库认证
function create_secret {
    scname=`kubectl get secret -n $ns|grep -w $secret_name|awk '{print $1}'|awk '{gsub(/^\s+|\s+$/, "");print}'`
    if [ "$scname" != $secret_name ];then
        kubectl create secret docker-registry ${secret_name} --docker-username=${docker_username} --docker-password=${docker_password} --docker-email=${docker_email} --docker-server=${docker_server} -n $ns
        echo -e "\033[32;32mdocker镜像仓库认证文件创建成功 \033[0m \n"
    else
        echo -e "\033[32;32mdocker镜像仓库认证文件已存在 \033[0m \n"
    fi
}
#创建nginx-ingress
function create_ingress {
    if [ "$build_k8s" == yes ];then
        echo -e "\033[32;32m开始创建ingress-nginx \033[0m"
        kubectl apply -f ingress-nginx.yaml
        echo -e "\033[32;32mingress-nginx创建成功 \033[0m \n"
    fi
}
#创建ssl secret
function create_sslsecret {
    if [ "$ssl_key" == yes ];then
        sslsecret=`kubectl get secret -n $ns|grep -w $ssl_key_secret|awk '{print $1}'|awk '{gsub(/^\s+|\s+$/, "");print}'`
        if [ "$sslsecret" != $ssl_key_secret ];then
            echo -e "\033[32;32m开始创建ssl Secret \033[0m"
            kubectl create secret tls $ssl_key_secret --cert=./ssl/$cert_ssl --key=./ssl/$key_ssl -n ${ns}
            if [ $? -ne 0 ];then
                echo -e "\033[31m请查看k8s/ssl目录下是否已添加域名证书文件 \033[0m"
                exit 1
            fi
            echo -e "\033[32;32mssl Secret创建成功 \033[0m \n"
        else
            echo -e "\033[32;32mssl Secret文件已存在 \033[0m \n"
        fi
    fi
}
#创建mysql
function create_mysql {
    if [ "$build_k8s" == yes ];then
        echo -e "\033[32;32m开始创建mysql \033[0m"
        envsubst < mysql-pod.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32mmysql创建成功 \033[0m \n"    
    else
        envsubst < mysql-svc.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32mmysql-svc创建成功 \033[0m \n"
    fi
}
#创建mongodb
function create_mongodb {
    if [ "$mongodb_create" == yes ];then 
        echo -e "\033[32;32m开始创建mongodb \033[0m"
        envsubst < mongodb-pod.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32mmongodb创建成功 \033[0m \n"
    else
        envsubst < mongodb-svc.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32mongodb-svc创建成功 \033[0m \n"  
    fi
}
#创建redis
function create_redis {
    if [ $redis_create == yes ];then
        echo -e "\033[32;32m开始创建redis \033[0m"
        envsubst < redis-pod.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32mredis创建成功 \033[0m \n"
    fi
}
#创建elasticsearch
function create_es {
    if [ $es_create == yes ];then
        echo -e "\033[32;32m开始创建elasticsearch \033[0m"
        envsubst < elasticsearch-pod.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32melasticsearch创建成功 \033[0m \n"
    fi
}
#创建emqtt
function create_emqtt {
    if [ $emqtt_create == yes ];then
        echo -e "\033[32;32m开始创建emqtt \033[0m"
        cat emqtt-pod.yaml ../jenkinsfile/ssl.txt |envsubst| kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32memqtt创建成功,含ingress域名 \033[0m \n"
    else
        envsubst < emqtt-pod.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
        echo -e "\033[32;32memqtt创建成功,不含ingress域名 \033[0m \n"
    fi
}
#创建img
function create_img {
    if [ $ssl_key == yes ];then
        echo -e "\033[32;32m开始创建img \033[0m"
        cat img-pod.yaml ../jenkinsfile/ssl.txt |envsubst| kubectl --kubeconfig /root/.kube/config-master apply -f -'
    else
        envsubst < img-pod.yaml | kubectl --kubeconfig /root/.kube/config-master apply -f -'
    fi
        echo -e "\033[32;32mimg创建成功 \033[0m \n"
}

function main {
    create_ns
    create_secret
  # create_ingress
  # create_sslsecret
    create_mysql
    create_mongodb
    create_redis
    create_es
    create_emqtt
    create_img
}
main
